//
//  HMChangeCityController.m
//  HMMeituanHD
//
//  Created by apple on 2017/5/19.
//  Copyright © 2017年 itheima. All rights reserved.
//

#import "HMChangeCityController.h"
#import "HMCityGroupModel.h"
#import "HMSearchResultController.h"



@interface HMChangeCityController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>
//分组城市数据
@property (nonatomic, strong) NSArray<HMCityGroupModel *> *cityGroups;
//列表视图
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//遮罩视图
@property (weak, nonatomic) IBOutlet UIButton *maskButton;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
//查询结果控制器  不需要频繁创建/销毁 内存缓存
@property (nonatomic, strong) HMSearchResultController *searchResultVc;

@end

@implementation HMChangeCityController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //设置导航栏
    [self setupNav];
    //获取数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cityGroups.plist" ofType:nil];
    NSArray *dictArr = [NSArray arrayWithContentsOfFile:path];
    self.cityGroups = [NSArray yy_modelArrayWithClass:[HMCityGroupModel class] json:dictArr];
    //设置UI
    [self setupUI];
}

- (void)setupUI{

    //注册Cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //设置tableView的快速索引颜色
    self.tableView.sectionIndexColor = HMColor(87, 186, 175);
    //设置搜索栏着色(会修改取消按钮的颜色)
    self.searchBar.tintColor = HMColor(87, 186, 175);
    //设置系统控件的文字语言需要通过修改info.plist中的开发区域
}

//设置导航栏
- (void)setupNav{
    //设置标题
    self.title = @"切换城市";
    //设置返回Item
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barBuutonItemWithTarget:self action:@selector(clickCloseItem) icon:@"btn_navigation_close" highlighticon:@"btn_navigation_close_hl"];
}

#pragma mark - 响应事件

- (void)clickCloseItem{

    [self dismissViewControllerAnimated:YES completion:nil];
}

//点击遮罩按钮
- (IBAction)clickMaskButton:(id)sender {
    
    [self cancelEditing];
}


#pragma mark - UISearchBarDelegate

//开始编辑后调用
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{

    //隐藏导航栏
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    //显示取消按钮
    searchBar.showsCancelButton = YES;
    //修改搜索栏背景图片
    searchBar.backgroundImage = [UIImage imageNamed:@"bg_login_textfield_hl"];
    //显示遮罩
    self.maskButton.hidden = NO;
}

//搜索栏中的内容发生变化后调用
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    //判断搜索栏中是否有内容,控制搜索结果视图的显隐
    if (searchBar.text.length) {
        
        self.searchResultVc.view.hidden = NO;
        //执行查询
        [self.searchResultVc fetchKeyword:searchBar.text];
        
    }else {
        
        self.searchResultVc.view.hidden = YES;
    }
}

//点击取消按钮后调用
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{

    [self cancelEditing];
    //清空搜索栏内容
    searchBar.text = nil;
    //隐藏搜索结果视图
    self.searchResultVc.view.hidden = YES;
}


//取消编辑调用
- (void)cancelEditing{

    //显示导航栏
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //隐藏取消按钮
    self.searchBar.showsCancelButton = NO;
    //修改搜索栏背景图片
    self.searchBar.backgroundImage = [UIImage imageNamed:@"bg_login_textfield"];
    //隐藏遮罩
    self.maskButton.hidden = YES;
    //取消编辑
    [self.searchBar endEditing:YES];
}

#pragma mark - UITableViewDataSource

//设置组数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.cityGroups.count;
}

//设置组头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return self.cityGroups[section].title;
}

//每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.cityGroups[section].cities.count;
}

//设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //设置数据
    cell.textLabel.text = self.cityGroups[indexPath.section].cities[indexPath.row];
    
    return cell;
}

//设置快速索引
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    //获取数组中每个元素的某个成员变量组成的数组
   return [self.cityGroups valueForKey:@"title"];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    //发送通知  切换城市&查询结果控制器都需要传递事件给homeVc 多对一使用通知
    [HMNoteCenter postNotificationName:HMCityDidChangeNote object:nil userInfo:@{HMCityDidChangeNoteCityName: self.cityGroups[indexPath.section].cities[indexPath.row]}];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - 懒加载


- (HMSearchResultController *)searchResultVc{
    if (_searchResultVc == nil) {
        _searchResultVc = [HMSearchResultController new];
        //设置父子控制器关系
        //添加子控制器  只是响应者链条包含子控制器
        [self addChildViewController:_searchResultVc];
        //显示内容视图
        [self.view addSubview:_searchResultVc.view];
        //添加子视图 一定设置尺寸/约束
        [_searchResultVc.view mas_makeConstraints:^(MASConstraintMaker *make) {
            //位置&宽高保持一致
            make.edges.equalTo(self.tableView);
        }];
        //设置默认隐藏搜索结果视图
        _searchResultVc.view.hidden = YES;
    }
    return _searchResultVc;
}

@end
